package cn.edu.scau.cmi.houzhicong.combinationPattern.gui;

import cn.edu.scau.cmi.houzhicong.combinationPattern.consistentPattern.ConsistentComponent;
import cn.edu.scau.cmi.houzhicong.combinationPattern.consistentPattern.ConsistentOrganization;
import cn.edu.scau.cmi.houzhicong.combinationPattern.consistentPattern.ConsistentPerson;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

public class ConsistentCombinationController implements Initializable {

    Map<String, ConsistentComponent> componentMap = new HashMap<>();

    @FXML
    private TextField parentText;
    @FXML
    private TextField childText;
    @FXML
    private TextField infoArea;
    @FXML
    private Button generateComponent;
    @FXML
    private Button addChild;
    @FXML
    private Button removeChild;
    @FXML
    private Button showChildren;
    @FXML
    private Button showComponent;

    @Override
    public void initialize(URL location, ResourceBundle resources) {}

    public void generateComponent(){
        StringBuilder sb = new StringBuilder();
        String parentName = parentText.getText();
        String childName = childText.getText();
        if(!parentName.equals("")) {
            if (componentMap.containsKey(parentName)) {
                sb.append(parentName + "已存在，不可再创建。");
            } else {
                componentMap.put(parentName, new ConsistentOrganization(parentName));
                sb.append(parentName + "组织已成功创建。");
            }
        }
        if(!childName.equals("")) {
            if (componentMap.containsKey(childName)) {
                sb.append(childName + "已存在，不可再创建。");
            } else {
                componentMap.put(childName, new ConsistentPerson(childName));
                sb.append(childName + "组织已成功创建。");
            }
        }
        infoArea.setText(sb.toString());
    }


    public void addChild(){
        String parentName = parentText.getText();
        String childName = childText.getText();
        if(parentName.equals("")){
            infoArea.setText("请输入父组织。");
        }else if(childName.equals("")){
            infoArea.setText("请输入子组织");
        }else if(!componentMap.containsKey(parentName)){
            infoArea.setText("父组织不存在。");
        }else if(!componentMap.containsKey(childName)){
            infoArea.setText("子组织不存在");
        }else{
            ConsistentComponent parent = componentMap.get(parentName);
            ConsistentComponent child = componentMap.get(childName);
            if(parent.getClass()!=ConsistentOrganization.class){
                infoArea.setText("此为个人，已不可再分。");
                return;
            }
            ((ConsistentOrganization)parent).addChild(child);
            infoArea.setText("添加成功");
        }
    }


    public void removeChild(){
        String parentName = parentText.getText();
        String childName = childText.getText();
        if(parentName.equals("")){
            infoArea.setText("请输入父组织。");
        }else if(childName.equals("")){
            infoArea.setText("请输入子组织");
        }else if(!componentMap.containsKey(parentName)){
            infoArea.setText("父组织不存在。");
        }else if(!componentMap.containsKey(childName)){
            infoArea.setText("子组织不存在");
        }else{
            ConsistentComponent parent = componentMap.get(parentName);
            ConsistentComponent child = componentMap.get(childName);
            if(parent.getClass()!=ConsistentOrganization.class){
                infoArea.setText("此为个人，已不可再分。");
                return;
            }else if(child.getParent()!=parent){
                infoArea.setText("两组织不存在从属关系");
                return;
            }
            ((ConsistentOrganization)parent).removeChild(child);
            infoArea.setText("删除成功");
        }
    }



    public void showChildren(){
        String parentName = parentText.getText();
        if(parentName.equals("")){
            infoArea.setText("请输入父组织。");
        }else if(!componentMap.containsKey(parentName)){
            infoArea.setText("该节点不存在");
        }else{
            ConsistentComponent parent = componentMap.get(parentName);
            if(parent.getClass()!= ConsistentOrganization.class){
                infoArea.setText("该节点不存在");
                return;
            }
            infoArea.setText(((ConsistentOrganization)parent).showChildren());
        }
    }


    public void showComponent(){
        StringBuilder sb = new StringBuilder();
        for (String componentName :
                componentMap.keySet()) {
            sb.append(componentName).append(", ");
        }
        String retString = sb.toString();
        if(retString.length()>=2){
            infoArea.setText(retString.substring(0,retString.length()-2));
        }else{
            infoArea.setText(retString);
        }
    }

}
